const app = getApp()
const $api = require('../../utils/api.js').API;
/******
 * role 1:未注册
 * 200:注册人用户,正在审核中
 * 300:注册机构用户正在审核中
 * 400:提交注册申请,未通过
 * 2:个人
 * 3:机构
 * 4:代理商
 */
Page({
    data: {
        city:'全部',
        list:[],
        educations:['全部','高中以下','中专','大专','本科','硕士以上'],
        educationtext:'学历',

        jobtext:'',
        index:0,
        citytext:'',
        region: ['天津市', '天津市', '全部'],
        customItem: '全部',
        //types:['全部','全职','兼职'],
        array: ['全部', '1年', '2年', '3年','4年','5年以上'],

        typetext:'',
        index1:0,
        jobCate:[
            {
                'id':0,
                'name':'全部',
                'addtime':'2020-10-20'
            }
        ],
        money:'',
        page:1,
        pageShow:true,
        cateName:'',
        star:0,
        hot:[],
        imgUrl:app.globalData.imgUrl,
        role:wx.getStorageSync('role')
    },
    onLoad: function (e){
        if(e.star){
            this.setData({
                star:e.star
            })
        }
        wx.showLoading()
        this.getJobCate();
        this.getList();
        this.getHot();
    },
    getHot:function(){
        var data = {}
        var that = this
        var hot = that.data.jobCate
        $api.get('job/hot',data).then(res => {
            //请求成功
            if(res.data.code == 1){
              that.setData({
                hot:hot.concat(res.data.data)
              })
            }
        }).catch(err => {
            //请求失败
            //console.log('job','请求失败');
        })
    },
    getJobCate:function(){
        var data = {}
        var that = this
        var jobCate = that.data.jobCate
        $api.get('job/list',data).then(res => {
            //请求成功
            if(res.data.code == 1){
              that.setData({
                jobCate:jobCate.concat( res.data.data)
              })
            }
        }).catch(err => {
            //请求失败
            console.log('job','请求失败');
        })
    },
    getList:function(){
        if(this.data.city == '全部'){
            var city = '';
        }else{
            var city = this.data.city;
        }
        var data = {
            'page':this.data.page,
            'cateName':this.data.cateName,
            'city':this.data.citytext,
            'workType':this.data.typetext,
            'star':this.data.star,
            'city':city
        }
        var that = this
        var list = this.data.list
        $api.get('member/list',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                if(res.data.data.last_page <= that.data.page){
                    that.setData({
                        pageShow:false
                    })
                }
                wx.hideLoading()
                if(that.data.page == 1){
                    that.setData({
                        list:res.data.data.data
                    })
                }else{
                    that.setData({
                        list:list.concat(res.data.data.data)
                    })
                }
            }
        }).catch(err => {
            //请求失败
            //console.log('member','请求失败');
        })
    },
    toSearch:function(e){
        var name = e.currentTarget.dataset.name
        if(name == '全部'){
            var catename = '';
        }else{
            var catename = name;
        }
        this.setData({
            cateName:catename,
            page:1,
            pageShow:true
        })
        wx.showLoading()
        this.getList();
    },
    jobChange:function(e){
        var name = this.data.jobCate[e.detail.value]['name']
        if(name == '全部'){
            var catename = '';
        }else{
            var catename = name;
        }
        this.setData({
            index1: e.detail.value,
            jobtext:name,
            cateName:catename,
            page:1,
            pageShow:true
         })
        wx.showLoading()
        this.getList();
    },
    bindRegionChange: function (e) {
        var name = e.detail.value[1];
        if(name !== '全部'){
            var city = e.detail.value[1];
        }else{
            if(e.detail.value[0] == '全部'){
                var city = '';
            }else{
                var city = e.detail.value[0];
            }
        }
        this.setData({
            region:e.detail.value,
            citytext: city,
            page:1,
            pageShow:true
        })
        wx.showLoading()
        this.getList();
    },

    typeChange:function(e){
        var name = this.data.array[e.detail.value]
        if(name == '全部'){
            var typename = '';
        }else{
            var typename = this.data.array[e.detail.value]
        }
        this.setData({
            page:1,
            index1:e.detail.value,
            typetext: typename,
            pageShow:true
        })
        wx.showLoading()
        this.getList();
    },
    toWorkerDetail:function(e){
        var role = wx.getStorageSync('role')
        if(role == 1){//未注册
            wx.navigateTo({
                url: '/pages/login/index'
            })
            return;
        }
        if(role == 2 || role == 4){ //个人用户 代理商
            wx.showToast({
                title:'暂无权限',
                duration:3000,
                mask:true,
                icon:'none', 
            })
            return;
        }
        var uid = e.currentTarget.dataset.uid
        var data = {'uid':wx.getStorageSync('uid')}
        $api.get('member/status',data).then(res => {
            if(res.data.data.status !== 2){
                wx.navigateTo({
                    url: '/pages/job/info?uid='+uid
                })
                return;
            }
            if(res.data.data.status == 2){
                var msg = '审核未通过,如有疑问请联系客服!!!'
            }
            if(res.data.data.status == 1){
                var msg = '暂未审核,请稍后!!!'
            }
            wx.showToast({
                title:msg,
                duration:3000,
                mask:true,
                icon:'none', 
            })
            return;
        })
    },
    getSee:function(id){
        var data = {'uid':wx.getStorageSync('uid')}
        $api.get('company/see',data).then(res => {
            console.log(res);
            return;
            if(res.data.data.status == 3){
                wx.navigateTo({
                    url: '/pages/job/info?id='+id
                })
                return;
            }
            if(res.data.data.status == 2){
                var msg = '审核未通过,如有疑问请联系客服!!!'
            }
            if(res.data.data.status == 1){
                var msg = '暂未审核,请稍后!!!'
            }
            wx.showToast({
                title:msg,
                duration:3000,
                mask:true,
                icon:'none', 
            })
                return;
          })
    },
    onReachBottom: function(t){
        if(this.data.pageShow){
            //当前不是最后一页可加载
            this.setData({
                page: this.data.page + 1,
            })
            //wx.showNavigationBarLoading()
            wx.showLoading()
            this.getList();
        }
    },
    onShareAppMessage: function() {
        return {
            title: "招人才-天意美业直通车",
            path: "/pages/index/index?puid="+wx.getStorageSync('uid')
        };
    },
    slectCity:function(){
        wx.navigateTo({
            url: '/pages/selectCity/index'
        })
    },
    jump:function(){
        wx.navigateTo({
            url: '/pages/vip/index'
        })
    }
});